package com.skyjing.toolsuitls.java.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v4.util.SimpleArrayMap;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

@SuppressLint("ApplySharedPref")
public class SpUitls {
    private static Context mContext;
    private static final SimpleArrayMap<String, SpUitls> SP_UTILS_MAP = new SimpleArrayMap<>();
    private SharedPreferences sp;

    public static SpUitls getInstance(/*Context context,*/) {
        return getInstance("", Context.MODE_PRIVATE);
    }

    public static SpUitls getInstance(/*Context context,*/int mode) {
        return getInstance( "", mode);
    }

    public static SpUitls getInstance(/*Context context,*/ String spName) {
        return getInstance(spName, Context.MODE_PRIVATE);
    }

    public static SpUitls getInstance(/*Context context,*/ String spName, int mode) {
        mContext = Utils.getApp();
        if (isEmpty(spName)) spName = "SpUitls";
        SpUitls spUtils = SP_UTILS_MAP.get(spName);
        if (spUtils == null) {
            spUtils = new SpUitls(spName, mode);
            SP_UTILS_MAP.put(spName, spUtils);
        }
        return spUtils;
    }

    private SpUitls(String spName) {
        sp = mContext.getSharedPreferences(spName, Context.MODE_PRIVATE);
    }

    private SpUitls(String spName, int mode) {
        sp = mContext.getSharedPreferences(spName, mode);
    }

    private static boolean isEmpty(String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public void put(@NonNull String key, Object value) {
        put(key, value, false);
    }

    public void put(@NonNull String key, Object value, boolean isCommit) {
        if (isCommit) {
            if (value instanceof String) {
                sp.edit().putString(key, (String) value).commit();
            } else if (value instanceof Long) {
                sp.edit().putLong(key, (Long) value).commit();
            } else if (value instanceof Boolean) {
                sp.edit().putBoolean(key, (Boolean) value).commit();
            } else if (value instanceof Float) {
                sp.edit().putFloat(key, (Float) value).commit();
            } else if (value instanceof Integer) {
                sp.edit().putInt(key, (Integer) value).commit();
            } else {
                sp.edit().putStringSet(key, (Set<String>) value).commit();
            }
        } else {
            if (value instanceof String) {
                sp.edit().putString(key, (String) value).apply();
            } else if (value instanceof Long) {
                sp.edit().putLong(key, (Long) value).apply();
            } else if (value instanceof Boolean) {
                sp.edit().putBoolean(key, (Boolean) value).apply();
            } else if (value instanceof Float) {
                sp.edit().putFloat(key, (Float) value).apply();
            } else if (value instanceof Integer) {
                sp.edit().putInt(key, (Integer) value).apply();
            } else {
                sp.edit().putStringSet(key, (Set<String>) value).apply();
            }
        }
    }

    public String getString(String key) {
        return getString(key, "");
    }

    public String getString(String key, String defValue) {
        return sp.getString(key, defValue);
    }

    public Boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    public Boolean getBoolean(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    public int getInt(String key) {
        return getInt(key, 0);
    }

    public int getInt(String key, int defValue) {
        return sp.getInt(key, defValue);
    }

    public float getFloat(String key) {
        return getFloat(key, 0);
    }

    public float getFloat(String key, float defValue) {
        float value = 0;
        if (sp != null) {
            value = sp.getFloat(key, defValue);
        }
        return value;
    }

    public long getLong(String key) {
        return getLong(key, 0);
    }

    public long getLong(String key, long defValue) {
        long value = 0;
        if (sp != null) {
            value = sp.getLong(key, defValue);
        }
        return value;
    }


    public Set<String> getStringSet(@NonNull String key) {
        return getStringSet(key, Collections.<String>emptySet());
    }

    public Set<String> getStringSet(@NonNull String key,
                                    Set<String> defaultValue) {
        return sp.getStringSet(key, defaultValue);
    }

    public Map<String, ?> getALl() {
        return sp.getAll();
    }

    public boolean contains(@NonNull String key) {
        return sp.contains(key);
    }

    public void remove(@NonNull String key) {
        remove(key, false);
    }

    public void remove(@NonNull String key, boolean isCommit) {
        if (isCommit) {
            sp.edit().remove(key).commit();
        } else {
            sp.edit().remove(key).apply();
        }
    }

    public void clear() {
        clear(false);
    }

    public void clear(boolean isCommit) {
        if (isCommit) {
            sp.edit().clear().commit();
        } else {
            sp.edit().clear().apply();
        }
    }
}
